package com.ruoyi.jl_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.jl_jl.domain.bo.JlJlBsQtjlBo;
import com.ruoyi.jl_jl.domain.vo.JlJlBsQtjlVo;
import com.ruoyi.jl_jl.domain.JlJlBsQtjl;
import com.ruoyi.jl_jl.mapper.JlJlBsQtjlMapper;
import com.ruoyi.jl_jl.service.IJlJlBsQtjlService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 其他计量Service业务层处理
 *
 * @author zyb
 * @date 2024-12-23
 */
@RequiredArgsConstructor
@Service
public class JlJlBsQtjlServiceImpl implements IJlJlBsQtjlService {

    private final JlJlBsQtjlMapper baseMapper;

    /**
     * 查询其他计量
     */
    @Override
    public JlJlBsQtjlVo queryById(Long qtjlId){
        return baseMapper.selectVoById(qtjlId);
    }

    /**
     * 查询其他计量列表
     */
    @Override
    public TableDataInfo<JlJlBsQtjlVo> queryPageList(JlJlBsQtjlBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JlJlBsQtjl> lqw = buildQueryWrapper(bo);
        Page<JlJlBsQtjlVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询其他计量列表
     */
    @Override
    public List<JlJlBsQtjlVo> queryList(JlJlBsQtjlBo bo) {
        LambdaQueryWrapper<JlJlBsQtjl> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JlJlBsQtjl> buildQueryWrapper(JlJlBsQtjlBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JlJlBsQtjl> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getQsId() != null, JlJlBsQtjl::getQsId, bo.getQsId());
        lqw.eq(bo.getContractId() != null, JlJlBsQtjl::getContractId, bo.getContractId());
        lqw.eq(bo.getBdId() != null, JlJlBsQtjl::getBdId, bo.getBdId());
        lqw.between(params.get("beginJlDate") != null && params.get("endJlDate") != null,
            JlJlBsQtjl::getJlDate ,params.get("beginJlDate"), params.get("endJlDate"));
        return lqw;
    }

    /**
     * 新增其他计量
     */
    @Override
    public Boolean insertByBo(JlJlBsQtjlBo bo) {
        JlJlBsQtjl add = BeanUtil.toBean(bo, JlJlBsQtjl.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setQtjlId(add.getQtjlId());
        }
        return flag;
    }

    /**
     * 修改其他计量
     */
    @Override
    public Boolean updateByBo(JlJlBsQtjlBo bo) {
        JlJlBsQtjl update = BeanUtil.toBean(bo, JlJlBsQtjl.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JlJlBsQtjl entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除其他计量
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
